package com.teng.smartdrawer.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.teng.smartdrawer.R;
import com.teng.smartdrawer.bean.Component;
import com.teng.smartdrawer.bean.Module;
import com.teng.smartdrawer.bean.Part;
import com.teng.smartdrawer.bean.Recommend;
import com.teng.smartdrawer.listener.DeleteListener;
import com.teng.smartdrawer.parser.DataParser;
import com.teng.smartdrawer.popUtils.ScreenUtils;
import com.teng.smartdrawer.utils.ImageLoadingConfig;
import com.teng.smartdrawer.view.CircleImageView;

import java.util.List;

/**
 * Created by teng on 16/4/1.
 */
public class GridAdapter extends BaseAdapter {

    private Context context;
    private List<Recommend> mData;

    private DeleteListener deleteListener;

    private int isShowDelete;

    private Animation anim;

    private ImageLoader imageLoader = ImageLoader.getInstance();
    private DisplayImageOptions imgOptions = ImageLoadingConfig.generateDisplayImageOptionsWithAnimationAndNoCorner(R.mipmap.ic_launcher);


    public GridAdapter(Context context, List<Recommend> datas) {
        this.context = context;
        this.mData = datas;

//        anim=new RotateAnimation(-10, 10,
//                Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
//        anim.setDuration(70);
//        anim.setRepeatMode(Animation.REVERSE);//反向重复
//        anim.setRepeatCount(Animation.INFINITE);//无限次

    }

    @Override
    public int getCount() {
        int ret = 0;
        if (mData != null) {
            ret = mData.size();
        }
        return ret;
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public DeleteListener getDeleteListener() {
        return deleteListener;
    }

    public void setDeleteListener(DeleteListener deleteListener) {
        this.deleteListener = deleteListener;
    }

    public int getIsShowDelete() {
        return isShowDelete;
    }

    public void setIsShowDelete(int isShowDelete) {
        this.isShowDelete = isShowDelete;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret = null;
        Recommend recommend = mData.get(position);

        if (recommend instanceof Part){
            ret = bindPartView(position, convertView, parent);
        }else {
            ret = bindModuleView(position, convertView, parent);
        }

        return ret;
    }

    View bindModuleView(final int position, View convertView, ViewGroup parent) {
        View ret = null;
        Module module = (Module) mData.get(position);
        if (convertView != null) {
            ret = convertView;
        }else{
            ret = LayoutInflater.from(context).inflate(R.layout.grid_item , parent , false);
        }

        PartViewHolder partViewHolder = (PartViewHolder) ret.getTag();
        if (partViewHolder == null) {
            partViewHolder = new PartViewHolder();
            partViewHolder.partName = (TextView) ret.findViewById(R.id.name_recommend_item);
            partViewHolder.imageView = (ImageView) ret.findViewById(R.id.image);
            partViewHolder.delete = (ImageView) ret.findViewById(R.id.delete);
            partViewHolder.parent_layout = (LinearLayout) ret.findViewById(R.id.parent_layout);

            ////////////   设置图片的高度  屏幕宽度的1/3 ///////////////////
//            int windowWidth = context.getResources().getDisplayMetrics().widthPixels;
//            int width = (windowWidth) / 3;
//            LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT);
//            partViewHolder.parent_layout.setLayoutParams(imageParams);

            ret.setTag(partViewHolder);
        }

        if (isShowDelete == 0){
            partViewHolder.delete.setVisibility(View.GONE);
        }else {
            partViewHolder.delete.setVisibility(View.VISIBLE);
        }

        partViewHolder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteListener.deleteRecommend(mData.get(position));
            }
        });

        partViewHolder.partName.setText(module.getD_name()+"");
        String pic_url = module.getD_img();

        if (pic_url == null) {
            pic_url = "http://img2.pconline.com.cn/pconline/0706/19/1038447_34.jpg";
        }

        imageLoader.displayImage(pic_url, partViewHolder.imageView, imgOptions);

        return ret;
    }


    /**
     *  加载  推荐的零件
     * @param position
     * @param convertView
     * @param parent
     * @return
     */
    View bindPartView(final int position, View convertView, ViewGroup parent) {
        View ret = null;
        Part component = (Part) mData.get(position);
        if (convertView != null) {
            ret = convertView;
        }else{
            ret = LayoutInflater.from(context).inflate(R.layout.grid_item , parent , false);
        }

        PartViewHolder partViewHolder = (PartViewHolder) ret.getTag();
        if (partViewHolder == null) {
            partViewHolder = new PartViewHolder();
            partViewHolder.partName = (TextView) ret.findViewById(R.id.name_recommend_item);
            partViewHolder.imageView = (ImageView) ret.findViewById(R.id.image);
            partViewHolder.delete = (ImageView) ret.findViewById(R.id.delete);
            partViewHolder.parent_layout = (LinearLayout) ret.findViewById(R.id.parent_layout);

            ////////////  屏幕宽度的1/3 ///////////////////
//            int windowWidth = context.getResources().getDisplayMetrics().widthPixels;
//            int width = (windowWidth) / 3;
//            LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT);
//            partViewHolder.parent_layout.setLayoutParams(imageParams);

            ret.setTag(partViewHolder);
        }

        if (isShowDelete == 0){
            partViewHolder.delete.setVisibility(View.GONE);
        }else {
            partViewHolder.delete.setVisibility(View.VISIBLE);
        }

        partViewHolder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteListener.deleteRecommend(mData.get(position));
            }
        });

        partViewHolder.partName.setText(component.getT_name()+"");
        String pic_url = component.getT_picture();//component.get();

        if (pic_url == null) {
            pic_url = "http://img2.pconline.com.cn/pconline/0706/19/1038447_34.jpg";
        }

        imageLoader.displayImage(pic_url, partViewHolder.imageView , imgOptions);
        return ret;
    }

    private static class PartViewHolder{
        public TextView partName;
        private ImageView imageView , delete;
        private LinearLayout parent_layout;
    }


}
